<?php 
/*
**********************************************
JoomLeague NAVIGATION MENU MODULE 
Copyright (c) 2007 JoomLeague.de
**********************************************
This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; either version 2 of the License, or 
(at your option) any later version.

This header must not be removed. Additional contributions/changes
may be added to this header as long as no information is deleted.
**********************************************

* NAVIGATION MENU MODULE
*
* 2007-08-31 - Version 0.91
*
* Displays a navigation menu for selected project.
* JoomLeague Team in a selected Project 
* There are some options for this module, which can be set in the 
* "Parameters" section of the module in Administration.
*

**********************************************
Get the latest version of JoomLeague at:
http://www.joomleague.de
**********************************************
*/
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
?>

<script type="text/javascript"> 
function gotoproject(url) { 
  window.location.href=url;
} 
</script>

<?php
global $mainframe, $my;

require_once ($mosConfig_absolute_path.'/administrator/components/com_joomleague/joomleague.class.php');
require_once ($mosConfig_absolute_path.'/modules/mod_joomleague_navigation_menu/mod_joomleague_navigation_menu.class.php');

// get parameters from module configuration
$params = mosParseParams( $module->params );
$menuclass="mainlevel$params->class_sfx";

// get joomleague component id
if ($params->custom_item_id=='') {
	$query="SELECT id FROM #__menu WHERE link LIKE 'index.php?option=com_joomleague%' LIMIT 1";
	$database->setQuery($query);
	$JLItemid = $database->loadResult();
}
else $JLItemid=$params->custom_item_id;

//current project id from cookie or request
if (intval(mosGetParam($_REQUEST, 'p', 0)) > 0) 
  $project_id = intval(mosGetParam($_REQUEST, 'p', 0));
else if ($params->joomleague_nav_menu_cookie == 1 || $params->default_project_id > 0) {
  $project_id = (mosGetParam($_COOKIE, 'nav_menu_project_id', 0)>0) ? mosGetParam($_COOKIE, 'nav_menu_project_id', 0):$params->default_project_id;
}
else $project_id = 0;

$thisjl_func = mosGetParam($_REQUEST , 'func' , '');
$thisteamid=intval(mosGetParam($_REQUEST, 'ttid', 0));
$thisclubid=intval(mosGetParam($_REQUEST, 'cid', 0));
$thismode=intval(mosGetParam($_REQUEST, 'mode', ''));

// menu style
$ausgabehtml=array();
$thisnav=new mod_joomleague_navigation_menu();
switch ($params->menu_style){
	case "vert_indent":
		$ausgabehtml['beginn']='<table border="0" cellpadding="0" cellspacing="0" width="100%">';
		$ausgabehtml['menupunktbeginn']='<tr align="left"><td>';
		$ausgabehtml['menupunktende']='</td></tr>';
		$ausgabehtml['ende']='</table>';
	break;
	case "horiz_flat":
		$ausgabehtml['beginn']='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="nowrap">';
		if ($params->spacer) $ausgabehtml['menupunktbeginn']='<span class="mainlevel'.$menuclass.'"> '.$params->spacer.' </span>';
		else $ausgabehtml['menupunktbeginn']='';
		$ausgabehtml['menupunktende'].='';
		if ($params->end_spacer) $ausgabehtml['ende']='<span class="mainlevel'.$menuclass.'"> '.$params->end_spacer.' </span>';
		else $ausgabehtml['ende']='';
		$ausgabehtml['ende'].='</td></tr></table>';
	break;
	case "list_flat":
		$ausgabehtml['beginn']='<ul id="'.$menuclass.'">';
		$ausgabehtml['menupunktbeginn']='<li>';
		$ausgabehtml['menupunktende']='</li>';
		$ausgabehtml['ende']='</ul>';
	break;
	case "raw":
		$ausgabehtml['beginn']='<span style="float:'.$params->float_position.';">';
		$ausgabehtml['menupunktbeginn']='';
		$ausgabehtml['menupunktende']='';
		$ausgabehtml['ende']='</span>';
	break;
}

//load project, update cookie
if ( $project_id > 0 ) {
  $joomleague = new JoomLeague( $database );
  $joomleague->load($project_id);
  if ($params->joomleague_nav_menu_cookie == 1) setcookie("nav_menu_project_id", $project_id, time()+3600);
}

//show dropboxes
echo $ausgabehtml['beginn'];
if ($params->show_project_dropdown=="project") {
	$thisnav->project_dropdown ($project_id, $ausgabehtml, $params, $JLItemid);
}
else if ($params->show_project_dropdown=="season") {
	echo $ausgabehtml['menupunktbeginn'];
	 ?>
<form action="" name="season_league">
<select name="seasons" class="inputbox" onchange="updateleagues(this.options[this.options.selectedIndex].value, 1)" style="width: 99%;font-size:9px;">
<option value=""><?php echo $params->seasons_text;?></option>
<?php
	$jsadd="";
	$javascript="";
	$query = "SELECT id, name FROM #__joomleague_seasons ORDER BY ordering";
	$database->setQuery($query);
	if ($season_list=$database->loadObjectList()) {
		foreach ($season_list AS $season) {
        // get series associated to season in a published project  			
  			$query="SELECT p.id, p.league_id, l.name 
                       FROM #__joomleague AS p 
                       LEFT JOIN #__joomleague_leagues AS l 
                       ON l.id=p.league_id 
                       WHERE p.published=1 AND p.season_id=".$season->id." 
                       ORDER BY l.ordering";
  			$database->setQuery($query);
  			if ($league_list=$database->loadObjectList()) {
          // at least one project with this season is published, add season to box
          $javascript.="leagues[$season->id] = new Array();\n";
          $javascript.="leagues[$season->id][0] = new Array( 0, '$params->leagues_text', '');\n";
    			echo '<option value="'.$season->id.'"';
    			if (isset($joomleague)&&$season->id==$joomleague->season_id) echo " selected";
    			$seasonname = ($params->stringlength && strlen($season->name) > $params->stringlength ) ? substr($season->name,0,$params->stringlength)."..." : $season->name;
    			echo '>'.$seasonname.'</option>';
    			
    			// add corresponding league to leagues box
    			$i=1;
  				foreach ($league_list AS $leagues) {
  					$leaguename = ($params->stringlength&&strlen($leagues->name)>$params->stringlength) ? substr($leagues->name,0,$params->stringlength)."..." : $leagues->name;
  					$javascript.="leagues[$season->id][".$i++."] = new Array( $leagues->league_id,'$leaguename', '".sefRelToAbs('index.php?option=com_joomleague&func='.$params->project_start.'&Itemid='.$JLItemid.'&p='.$leagues->id)."');\n";
  					if ( isset($joomleague->league_id) && $joomleague->league_id==$leagues->league_id ) 
              $jsadd="updateleagues(".$joomleague->season_id.");";
  				}
  			}
  		}
		}
	?>
	</select>
	<select name="leagues" size="1" class="inputbox" style="width: 99%;font-size:9px;display:<?php echo (isset($joomleague->league_id))?'block':'none';?>;" onchange="javascript:if(this.options.selectedIndex!=0) gotoproject(this.options[this.options.selectedIndex].value)">
	<option style="display:none" value=""></option>
	</select>

	<script type="text/javascript">
// <![CDATA[ 
var seasonlist=document.season_league.seasons;
var leagueslist=document.season_league.leagues;
var leagues=new Array();

<?php
echo $javascript;
?>

function updateleagues(selectedseasongroup, temp) {
	if (selectedseasongroup==0) leagueslist.style.display="none";
	else leagueslist.style.display="block";
  leagueslist.options.length=0;
  <?php 
  if (isset($joomleague->league_id)) { 
    echo "  var selval=".$joomleague->league_id.";\n"; 
  }
  else echo "  var selval='';\n"; 
  ?>
  if ( selectedseasongroup>0 ) {
    for (i=0, count=leagues[selectedseasongroup].length ; i<count ; i++) {
    	leagueslist.options[leagueslist.options.length]=new Option(leagues[selectedseasongroup][i][1], leagues[selectedseasongroup][i][2]);    	  
      if (selval==leagues[selectedseasongroup][i][0]) leagueslist.options.selectedIndex=i;}
	}
	if (temp==1) leagueslist.options.selectedIndex=0;
}
<?php echo $jsadd;?>
// ]]>
</script>
</form>
<?php
echo $ausgabehtml['menupunktende'];
}
if (isset($joomleague)&&is_a($joomleague, 'JoomLeague')&&$project_id>0) {
	if ($params->show_teams_dropdown==1 || $params->show_clubs_dropdown==1) {
		$teamcondition = '';
		if ($params->use_fav == 1) $teamcondition = $joomleague->fav_team;
		else $teamcondition = $params->team_ids;
		  if ($params->user_fav_team == 1 && isset($_COOKIE['joomleague_user_fav_team_p_'.$joomleague->id.''])) $user_fav_team = $_COOKIE['joomleague_user_fav_team_p_'.$joomleague->id.''];
      if ($params->user_fav_team == 1 && !isset($_COOKIE['joomleague_user_fav_team_p_'.$joomleague->id.'']) && $my->id > 0) { 
        $query = sprintf("SELECT fav_team FROM #__joomleague_tip_members WHERE user_id = %s AND project_id = %s", $my->id, $joomleague->id);
        $database->setQuery($query);
        $user_db_fav_team = $database->loadResult();
        $query = sprintf("SELECT team_id FROM #__joomleague_team_joomleague WHERE id = %s AND project_id = %s", $user_db_fav_team, $joomleague->id);
        $database->setQuery($query);
        $user_fav_team = $database->loadResult();
        }
		$query = "SELECT tl.id AS teamtoolid, tl.standard_playground, tl.admin, tl.start_points, tl.info, tl.team_id, tl.checked_out, tl.picture, t.id, t.name, t.short_name, t.middle_name, t.description, t.club_id, u.username
                FROM #__joomleague_team_joomleague tl
           LEFT JOIN #__joomleague_teams t on tl.team_id = t.id
           LEFT JOIN #__users u on tl.admin = u.id
               WHERE tl.project_id = ".$project_id;
      if ($teamcondition != 'all' && isset ($user_fav_team)) $query.= " AND t.id IN (".$teamcondition.','.$user_fav_team.") ";
    else if ($teamcondition != 'all' && !isset ($user_fav_team))$query.= " AND t.id IN (".$teamcondition.") ";
    $query.= " ORDER BY t.name";
	 	$database->setQuery($query);
    $results = $database->loadObjectList();
	}
	if ($params->show_teams_dropdown==1) {
		$selectoptions=array();
		$idstring="ttid";
		$selectoptions[] = mosHTML::makeOption('', sprintf("%s", $params->text_teams_dropdown));
		$url_string='index.php?option=com_joomleague&amp;func=showPlayers&amp;Itemid='.$JLItemid.'&amp;p='.$joomleague->id;
		$url_string.='&amp;'.$idstring.'=';
		$selectedoptionsvalue='';
		if (count($results)>0) {
			foreach($results as $result) {
	 			$used_id=$result->teamtoolid;
	 			if ($used_id==$thisteamid) {
					$pathwaytext=$result->name;
					$selectedoptionsvalue=sefRelToAbs($url_string.$used_id);
					if ($pathwaytext!="") $mainframe->appendPathWay($pathwaytext);
				}
				$selectoptions[] = mosHTML::makeOption(sefRelToAbs($url_string.$used_id), sprintf("%s", $result->name));
			}
			if ($params->heading_teams_dropdown!="") echo $ausgabehtml['menupunktbeginn'].'<span class="'.$params->nav_group_style.'">'.$params->heading_teams_dropdown.'</span>'.$ausgabehtml['menupunktende'];
			$teamlist = mosHTML::selectList( $selectoptions, 'team', "class=\"inputbox\" onchange=\"javascript:if (this.options[this.options.selectedIndex].value!='') window.location.href=this.options[this.options.selectedIndex].value;\" style=\"font-size:9px;width:99%;\"",'value', 'text', $selectedoptionsvalue );
			echo $ausgabehtml['menupunktbeginn'].'<form action="">'.$teamlist.'</form>'.$ausgabehtml['menupunktende'];
		}
	}
	if ($params->show_clubs_dropdown==1) {
		$selectoptions=array();
		$selectedoptionsvalue='';
		$idstring="cid";
		$selectoptions[] = mosHTML::makeOption('', sprintf("%s", $params->text_clubs_dropdown));
		$url_string="index.php?option=com_joomleague&amp;func=showClubInfo&amp;p=$joomleague->id&amp;Itemid=$JLItemid";
		$url_string.="&amp;$idstring=";
		$selectedoptionsvalue='';
		if (count($results)>0) {
	  	foreach($results as $result) {
		  	$used_id=$result->club_id;
				$selectoptions[] = mosHTML::makeOption(sefRelToAbs($url_string.$used_id), sprintf("%s", $result->name));
				if ($used_id==$thisclubid) {
					$pathwaytext=$result->name;
					$selectedoptionsvalue=sefRelToAbs($url_string.$used_id);
					if ($pathwaytext!="") $mainframe->appendPathWay($pathwaytext);
				}
			}		
			if ($params->heading_clubs_dropdown!="") echo $ausgabehtml['menupunktbeginn'].'<span class="'.$params->nav_group_style.'"></span>'.$params->heading_clubs_dropdown.$ausgabehtml['menupunktende'].'';
			$teamlist = mosHTML::selectList( $selectoptions, 'team', "class=\"inputbox\" onchange=\"javascript:if (this.options[this.options.selectedIndex].value!='') window.location.href=this.options[this.options.selectedIndex].value;\" style=\"font-size:9px;width:99%\"",'value', 'text', $selectedoptionsvalue );
	  	echo $ausgabehtml['menupunktbeginn'].'<form action="">'.$teamlist.'</form>'.$ausgabehtml['menupunktende'];
		}
	}
if ($joomleague->id > 0 && $params->show_nav_links==1) {
	for ($x=1;$x<=16;$x++) { 
		$functext="navpoint$x";
		$labeltext="navpoint_label$x";
		if ($thismode!='') $thisjl_func.="&amp;mode=".$thismode;
		if ($params->$functext != "") {
			$thisnav->makeJLnavlinks ($joomleague, $ausgabehtml, $menuclass, str_replace("&", "&amp;", $params->$functext), $params->$labeltext, $params, $JLItemid, $thisjl_func);
		}
	}
}
}
echo $ausgabehtml['ende'];?>
